<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>{$title}</legend>
    </fieldset>
    <form class="layui-form layui-form-pane">
       <div class="layui-form-item">
            <label class="layui-form-label">字段类型</label>
            <div class="layui-input-4">
                <select id="type" name="type" lay-filter="type" class="required" lay-verify="required" {if condition="ACTION_NAME eq 'fieldedit'"}disabled{/if}>
                    <option value='' >请选择字段类型</option>
                    <option value="catid" {if $info != 'null' && $info['type']=='catid'} selected{/if}>栏目</option>
                    {if condition="ACTION_NAME eq 'fieldedit'"}<option value="title" {if $info != 'null' && $info['type']=='title'} selected{/if}>标题</option>{/if}
                    <option value="text" {if $info != 'null' && $info['type']=='text'} selected{/if}>单行文本</option>
                    <option value="textarea" {if $info != 'null' && $info['type']=='textarea'} selected{/if}>多行文本</option>
                    <option value="editor" {if $info != 'null' && $info['type']=='editor'} selected{/if}>编辑器</option>
                    <option value="select" {if $info != 'null' && $info['type']=='select'} selected{/if}>下拉列表</option>
                    <option value="radio" {if $info != 'null' && $info['type']=='radio'} selected{/if}>单选按钮</option>
                    <option value="checkbox" {if $info != 'null' && $info['type']=='checkbox'} selected{/if}>复选框</option>
                    <option value="image" {if $info != 'null' && $info['type']=='image'} selected{/if}>单张图片</option>
                    <option value="images" {if $info != 'null' && $info['type']=='images'} selected{/if}>多张图片</option>
                    <option value="file" {if $info != 'null' && $info['type']=='file'} selected{/if}>文件上传</option>
                    <option value="number" {if $info != 'null' && $info['type']=='number'} selected{/if}>数字</option>
                    <option value="datetime" {if $info != 'null' && $info['type']=='datetime'} selected{/if}>日期和时间</option>
                    <option value="posid" {if $info != 'null' && $info['type']=='posid'} selected{/if}>推荐位</option>
                    <option value="groupid" {if $info != 'null' && $info['type']=='groupid'} selected{/if}>会员组</option>
                    <option value="linkage" {if $info != 'null' && $info['type']=='linkage'} selected{/if}>地址联动</option>
                    <option value="template" {if $info != 'null' && $info['type']=='template'} selected{/if}>模板选择</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字段名</label>
            <div class="layui-input-4">
                <input type="text" name="field" value="{$info.field|default=''}" value="{$info.width|default=''}" lay-verify="required" placeholder="必填：字段名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">别名</label>
            <div class="layui-input-4">
                <input type="text" name="name" value="{$info.name|default=''}" placeholder="必填：别名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字段设置</label>
            <div class="layui-input-4" id="field_setup">

            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">class名称</label>
            <div class="layui-input-4">
                <input type="text" name="class" value="{$info.class|default=''}"  placeholder="class名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">必填</label>
            <div class="layui-input-block">
                <input type="radio" name="required" value="1" {if $info == 'null' || $info['required']==1} checked {/if}title="是">
                <input type="radio" name="required" value="0" {if $info != 'null' && $info['required']==0} checked {/if}title="否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">验证规则</label>
            <div class="layui-input-4">
                <select name="pattern">
                    <option value="">请选择验证规则</option>
                    {volist name="pattern" id='vo'}
                        <option value="{$vo.name}" {if $info != 'null' && $vo['name'] == $info['pattern']} selected{/if}>{$vo.title}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">字符长度</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="minlength" value="{$info.minlength|default='1'}" class="layui-input">
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="maxlength" value="{$info.maxlength|default='5'}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">错误信息</label>
            <div class="layui-input-4">
                <input type="text" name="errormsg" value="{$info.errormsg|default=''}" placeholder="验证失败错误信息" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">{:lang('submit')}</button>
                <a href="{:url('field',['id'=>$moduleid])}" class="layui-btn layui-btn-primary">{:lang('back')}</a>
            </div>
        </div>
    </form>
</div>

<script src="__PUBLIC__common/js/jquery.2.1.1.min.js"></script>
<script>
    var oldfield = '{$info.field|default=''}';
    var info = {$infos|raw};
    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer;
        //编辑事件
        if(info != 1){
           field_setting(info,'edit',form);
        }
        //事件监听
        form.on('select(type)', function(type){
            field_setting(type,'add',form);
        });
        form.on('submit(submit)', function (data) {
            var loading = layer.load(1, {shade: [0.1, '#fff']});
            data.field.moduleid = "{$moduleid}";
            if('{$info.id|default=''}'){
                data.field.oldfield = oldfield;
                data.field.id = '{$info.id|default=0}';
            }
            $.post("", data.field, function (res) {
                layer.close(loading);
                if (res.code > 0) {
                    layer.msg(res.msg, {time: 1800, icon: 1}, function () {
                        location.href = res.url;
                    });
                } else {
                    layer.msg(res.msg, {time: 1800, icon: 2});
                }
            });
        });
    });

    function field_setting(info,action,form) {
        if(action=='add'){
            var data =  '';
            var url =  "{:url('fieldAdd')}?isajax=1&moduleid={$moduleid}&type="+type.value;
        }else{
            var data = info.setup;
            var url =  "{:url('fieldAdd')}?isajax=1&moduleid={$moduleid}&type="+info.type+"&name="+info.field;
        }
        $.ajax({
            type: "POST",
            url: url,
            data: data,
            success: function(msg){
                $('#field_setup').html(msg);
                form.render();
            },
            error:function(){
            }
        });
    }
</script>